<?php
declare (strict_types = 1);

namespace app\auth\model;

/**
 * @mixin \think\Model
 */
class RightModel extends BaseModel
{
    protected $table = 'tb_rights';
    protected $append = ['type_str','createtime_str'];

    public function getCreateTimeStrAttr($value,$data)
    {
        if(empty($data['createtime'])) return 0;
        return date('Y-m-d H:i:s', $data['createtime']);
    }

    const WITH_CHILDS = 'childs';
    const WITH_PARENT = 'parent';
    public function childs(){
        return $this->hasMany('app\auth\model\RightModel', 'pid')->field(['id', 'name', 'pid', 'url', 'type', 'value']);
    }

    public function parent(){
        return $this->belongsTo('app\auth\model\RightModel', 'pid')->field(['id', 'name', 'pid', 'url', 'type', 'value']);
    }

    //根菜单
    const PID_BASE = 0;
    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 2;
    static $STATUS_STR = [
        self::STATUS_NORMAL => '正常',
        self::STATUS_DELETE => '冻结'
    ];

    const TYPE_MENU = 1;
    const TYPE_BTN = 2;
    const TYPE_OTHER = 3;
    static $TYPE_STR = [
        self::TYPE_MENU => '菜单',
        self::TYPE_BTN => '按钮',
        self::TYPE_OTHER => '其他'
    ];
    public function getTypeStrAttr($value,$data){
        return @self::$STATUS_STR[$data['type']];
    }
}
